
package com.yunfei.api.product;

import com.vdurmont.emoji.EmojiParser;
import com.yunfei.bo.prodAcceDtl.ProdAcceDtlBo;
import com.yunfei.bo.prodAcceDtl.ProdAccePingLunDtlBo;
import com.yunfei.bo.prodAcceDtl.ProdTagBo;
import com.yunfei.bo.prodActiInfo.ProdActiInfoBo;
import com.yunfei.bo.product.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.userOperLog.UserOperLogBo;
import com.yunfei.contants.ProductPingLunContants;
import com.yunfei.contants.UserCenterContants;
import com.yunfei.entity.TPersonalUserEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.service.ValidTokenService;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 产品页面接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "产品页面",tags = "产品页面")
@RestController
@RequestMapping("/api/product/page")
public class ProductPageApiController {
    @Autowired
    private VideoMapper videoMapperDao;//产品路演视频
    @Autowired
    private ProductPageMapper productPageDao;//产品详情
    @Autowired
    private ProductBeiAnMapper productBeiAnDao;//产品备案信息
    @Autowired
    private ProductTagMapper productTagDao;//产品标签
    @Autowired
    private ProductPingLunMapper productPingLunMapper; //产品评论
    @Autowired
    private ProductDetailMapper productDetailDao; //产品详情
    @Autowired
    private ProdAcceDtlMapper prodAcceDtlMapper; //产品附件
    @Autowired
    private ProdAnswAcceDtlMapper prodAnswAcceDtlMapper; //产品评论附件
    @Autowired
    private ValidTokenService validToken; //验证token是否合法
    @Autowired
    private UserOperLogMapper userOperLogMapper;//产品日志
    @Autowired
    private ProdActiInfoMapper prodActiInfoMapper;//产品日志
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;

    /**
    * @Description: 产品页面信息
    * @Author: zhengyunfei
    * @Date: 2019/1/11
    * @time:17:00
    */
    @GetMapping("/get")
    @ApiOperation(value = "产品页面",notes = "产品页面",position = 1,response = Object.class)
    public Object get(String projId,String token){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        //if (validToken.validToken(token, resultModel)) return resultModel;
        String videoUrl="";
        //1.查询路演视频
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("projId",projId);
        Map<String,Object> videoQueryMap=new HashMap<>();
        videoQueryMap.put("projId",projId);
        videoQueryMap.put("showFlg","Y");
        List<ProdAcceDtlBo> prodAcceDtlBoList= (List<ProdAcceDtlBo>) prodAcceDtlMapper.get(videoQueryMap);
        if(!StringUtil.isNullOrEmpty(prodAcceDtlBoList)&&prodAcceDtlBoList.size()>0){
            ProdAcceDtlBo prodAcceDtlBo=prodAcceDtlBoList.get(0);
            videoUrl= prodAcceDtlBo.getAcceUrl();//路演视频地址
        }
        resultMap.put("videoUrl",videoUrl);
        String userType="";
        String userId= TokenUtil.getInstance().getUserId(token);
        TPersonalUserEntity tPersonalUserEntity=tPersonalUserMapper.findById(userId);
        if(!StringUtil.isNullOrEmpty(tPersonalUserEntity)){
            userType=tPersonalUserEntity.getUserType();
        }
        //2.查询产品信息
        ProductPageBo productPageBo=null;
        if(UserCenterContants.USER_TYPE_02.equals(userType)){
             productPageBo= (ProductPageBo) productPageDao.getDetailForLiCaiShi(queryMap);
        }else{
             productPageBo= (ProductPageBo) productPageDao.get(queryMap);
        }

        //保存产品信息
        resultMap.put("product",productPageBo);
        //3.查询产品备案信息
        ProductBeiAnBo productBeiAnBo= (ProductBeiAnBo) productBeiAnDao.get(queryMap);
        String productBeiAnUrl="";
        if(!StringUtil.isNullOrEmpty(productBeiAnBo)){
            productBeiAnUrl=productBeiAnBo.getBakInfoUrl();
        }
        resultMap.put("productBeiAnUrl",productBeiAnUrl);
        //4.相关评论-标签
        List<ProductTagBo> productTagBoList=productTagDao.get(queryMap);

        resultMap.put("productTagList",productTagBoList);
        //5.产品所有评论
        Map<String,Object> pinglunMap=new HashMap<>();
        pinglunMap.put("projId",projId);
        System.out.println("token===================================="+token);
        System.out.println("根据token获取用户userId========================================"+userId);
        pinglunMap.put("userId",userId);
        pinglunMap.put("pageNow",1);
        pinglunMap.put("pageSize", ProductPingLunContants.PAGE_SIZE);
        List<ProductPingLunBo> productPingLunList= productPingLunMapper.get(pinglunMap);
        for(int i=0;i<productPingLunList.size();i++){
            String answId=productPingLunList.get(i).getAnswId();
            String answDtl=productPingLunList.get(i).getAnswDtl();
            String answDtlParser=EmojiParser.parseToUnicode(answDtl);
            productPingLunList.get(i).setAnswDtl(answDtlParser);
            if(!StringUtil.isNullOrEmpty(answId)){
                //查询评论附件-图片
                Map<String,Object> prodAcceDtlQueryParam=new HashMap<>();
                prodAcceDtlQueryParam.put("answId",answId);
                List<ProdAccePingLunDtlBo> pingLunDtlList= prodAnswAcceDtlMapper.findPingLunDtlList(prodAcceDtlQueryParam);
                productPingLunList.get(i).setPingLunDtlList(pingLunDtlList);
                //查询产品评论标签
                List<ProdTagBo> tagNmList=prodAnswAcceDtlMapper.findProdTagNmList(prodAcceDtlQueryParam);
                productPingLunList.get(i).setProdTagList(tagNmList);
            }

        }
        resultMap.put("productPingLunList",productPingLunList);
        //插入操作日志--插入查看日志
      //--如果查看记录中没有此营销项目（下面sql结果为0）
        //SELECT COUNT(1)
        //  FROM PROD_ACTI_INFO
        // WHERE PROJ_ID = '' --传入营销项目编号
        //--插入表
        // INSERT INTO PROD_ACTI_INFO(PROJ_ID, CHICK_CNT) VALUES('', 1); --第一次被查看
        //COMMIT;
        //--如果记录中有此营销项目
        //--更新字段CHICK_CNT = CHICK_CNT+1
        //UPDATE PROD_ACTI_INFO SET CHICK_CNT = CHICK_CNT + 1 WHERE PROJ_ID = ''; --传入营销项目编号
        UserOperLogBo userOperLogBo=new UserOperLogBo();
        userOperLogBo.setBrowMdl("PROD");
        userOperLogBo.setUserId(userId);
        userOperLogBo.setBrowDtlId(projId);
        userOperLogMapper.add(userOperLogBo);
        ProdActiInfoBo prodActiInfoQueryParam=new ProdActiInfoBo();
        prodActiInfoQueryParam.setProjId(projId);
        int count=prodActiInfoMapper.count(prodActiInfoQueryParam);
        if(count==0){
            //如果查看记录中没有此营销项目
            //--插入表
            // INSERT INTO PROD_ACTI_INFO(PROJ_ID, CHICK_CNT) VALUES('', 1); --第一次被查看
            ProdActiInfoBo prodActiInfoAddParam=new ProdActiInfoBo();
            prodActiInfoAddParam.setProjId(projId);
            prodActiInfoAddParam.setChickCnt("1");
            prodActiInfoMapper.add(prodActiInfoAddParam);
        }else{
            //如果有此记录
            //--如果记录中有此营销项目
            //--更新字段CHICK_CNT = CHICK_CNT+1
            //UPDATE PROD_ACTI_INFO SET CHICK_CNT = CHICK_CNT + 1 WHERE PROJ_ID = ''; --传入营销项目编号
            ProdActiInfoBo prodActiInfoUpdateParam=new ProdActiInfoBo();
            prodActiInfoUpdateParam.setProjId(projId);
            prodActiInfoMapper.updateCount(prodActiInfoUpdateParam);

        }
        //读取查看次数：
        // SELECT CHICK_CNT  FROM PROD_ACTI_INFO WHERE PROJ_ID = ‘’传入营销项目编号

        int viewCount=prodActiInfoMapper.getViewCount(prodActiInfoQueryParam);
        resultMap.put("viewCount",viewCount);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }
    /**
    * @Description: 加载更多评论
    * @Author: zhengyunfei
    * @Date: 2019/2/12
    * @time:11:27
    */
    @GetMapping("/getMorePingLun")
    @ApiOperation(value = "加载更多评论",notes = "加载更多评论",position = 1,response = Object.class)
    public Object getMorePingLun(String projId,String token,int pageNow){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        //5.产品所有评论
        Map<String,Object> pinglunMap=new HashMap<>();
        pinglunMap.put("projId",projId);
        String userId= TokenUtil.getInstance().getUserId(token);
        pinglunMap.put("userId",userId);
        pinglunMap.put("pageNow",(pageNow-1)*ProductPingLunContants.PAGE_SIZE+1);
        pinglunMap.put("pageSize", pageNow*ProductPingLunContants.PAGE_SIZE);
        List<ProductPingLunBo>productPingLunList= productPingLunMapper.get(pinglunMap);
        for(int i=0;i<productPingLunList.size();i++){
            String answId=productPingLunList.get(i).getAnswId();
            if(!StringUtil.isNullOrEmpty(answId)){
                //查询评论附件-图片
                Map<String,Object> prodAcceDtlQueryParam=new HashMap<>();
                prodAcceDtlQueryParam.put("answId",answId);
                List<ProdAccePingLunDtlBo> pingLunDtlList= prodAnswAcceDtlMapper.findPingLunDtlList(prodAcceDtlQueryParam);
                productPingLunList.get(i).setPingLunDtlList(pingLunDtlList);
            }

        }
        resultMap.put("productPingLunList",productPingLunList);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }

}
